// output.sp - TF2Stern Data Output
//  by Thoams 'nickname' Roth

/* File Structure:
 *  SternOutputInit()
 *  SternOutputClose()
 *  SetOutputFilename helper
 *  OpenOutputFile()
 *  WriteOutput(message, variables...)
 *  CloseOutputFile()
 *  DeleteOutputFile()
 */

/////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
/////////////////////////////////////////////////////////////////////////////
new Handle:ghOutputFile = INVALID_HANDLE;

new String:gsOutputFilename[64] = "";
 
/////////////////////////////////////////////////////////////////////////////
// SternOutputInit()
//  initializes the output variables
/////////////////////////////////////////////////////////////////////////////
SternOutputInit()
{
  ghOutputFile = INVALID_HANDLE;
  gsOutputFilename[0] = '\0';
}

/////////////////////////////////////////////////////////////////////////////
// SternOutputClose()
//  closes open file handles prior to exit
/////////////////////////////////////////////////////////////////////////////
SternOutputClose()
{
  // if output is interrupted, delete the file
  if( ghOutputFile != INVALID_HANDLE )
  {
    DeleteOutputFile();
  }
}

/////////////////////////////////////////////////////////////////////////////
// SetOutputFilename helper
//  local function that generates a unique filename
/////////////////////////////////////////////////////////////////////////////
static SetOutputFilename()
{
  WriteDebug(dpFunc, "called SetOutputFilename()");
  decl String:mapname[32];
  decl String:timestr[16];
  new time = GetTime();
  
  // get filename variables
  GetCurrentMap(mapname, sizeof(mapname));
  FormatTime(timestr, sizeof(timestr), "%m-%d_%H-%M", time);
  
  // format and store filename
  Format(gsOutputFilename, sizeof(gsOutputFilename),
    "%s_%s.dat", timestr, mapname);
}

/////////////////////////////////////////////////////////////////////////////
// OpenOutputFile()
//  opens gsOutputFilename in override mode with the handle ghOutputFile
/////////////////////////////////////////////////////////////////////////////
OpenOutputFile()
{
  WriteDebug(dpFunc, "called OpenOutputFile()");

  // guard against bad function calls
  if( ghOutputFile == INVALID_HANDLE )
  {
    // generate filename
    SetOutputFilename();
    
    // create output file
    ghOutputFile = OpenFile(gsOutputFilename, "w");
    if( ghOutputFile == INVALID_HANDLE )
    {
      // discard filename on error
      gsOutputFilename[0] = '\0';
      WriteDebug(dpError, "unable to create file %s", gsOutputFilename);
    }
    else
    {
      WriteDebug(dpHigh, "created new file named %s", gsOutputFilename);
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// WriteOutput(message, variables...)
//  writes message to output file
/////////////////////////////////////////////////////////////////////////////
WriteOutput(const String:template[], any:...)
{
  WriteDebug(dpFunc, "called WriteOutput(message)");
  
  // guard against bad function calls
  if( ghOutputFile != INVALID_HANDLE )
  {
    decl String:message[128];
    
    // fill the format variables in the message template
    VFormat(message, sizeof(message), template, 2);
    WriteDebug(dpLow, "message = %s", message);
    
    // write data to file
    if( !WriteFileLine(ghOutputFile, message) )
    {
      WriteDebug(dpError, "unable to write to file %s", gsOutputFilename);
    }
    else
    {
      WriteDebug(dpLow, "wrote message to file %s", gsOutputFilename);
    }
  }
}

/////////////////////////////////////////////////////////////////////////////
// CloseOutputFile()
//  closes the file handle ghOutputFile
/////////////////////////////////////////////////////////////////////////////
CloseOutputFile()
{
  WriteDebug(dpFunc, "called CloseOutputFile()");
  
  // guard against bad function calls
  if( ghOutputFile != INVALID_HANDLE )
  {
    // close output file
    CloseHandle(ghOutputFile);
    ghOutputFile = INVALID_HANDLE;
    WriteDebug(dpHigh, "saved data to file %s", gsOutputFilename);
    
    // discard filename to prevent accidental deletion
    gsOutputFilename[0] = '\0';
  }
}

/////////////////////////////////////////////////////////////////////////////
// DeleteOutputFile()
//  closes the file handle ghOutputFile and deletes the file gsOutputFilename
/////////////////////////////////////////////////////////////////////////////
DeleteOutputFile()
{
  WriteDebug(dpFunc, "called DeleteOutputFile()");
  
  // guard against bad function calls
  if( gsOutputFilename[0] != '\0' )
  {
    // close file handle if necessary
    if( ghOutputFile != INVALID_HANDLE )
    {
      CloseHandle(ghOutputFile);
      ghOutputFile = INVALID_HANDLE;
    }
    
    // delete output file
    if( DeleteFile(gsOutputFilename) )
    {
      WriteDebug(dpHigh, "deleted file named %s", gsOutputFilename);
    }
    else
    {
      WriteDebug(dpError, "unable to delete file %s", gsOutputFilename);
    }
    
    // discard old filename
    gsOutputFilename[0] = '\0';
  }
}
